import {defHttp} from '/@/utils/http/axios';
import {Modal} from 'ant-design-vue';

enum Api {
  list = "/msg-push-service/msgPushTask/listPushTask",
  save = "/msg-push-service/msgPushTask/savePushTask",
  edit = "/msg-push-service/msgPushTask/updatePushTask",
  get = "/msg-push-service/msgPushTask/getPushTask",
  delete = "/msg-push-service/msgPushTask/deletePushTask",
  deleteBatch = "/msg-push-service/msgPushTask/deleteBatchPushTask",
  listMsgField = "/msg-push-service/msgField/listMsgField",
  saveOrUpdateMsgField = "/msg-push-service/msgField/batchSaveMsgField",
  listTemplateByTaskId = "/msg-push-service/msgTemplate/listTemplateByTaskId",
  listChannel = "/msg-push-service/msgPushTask/listChannel",

  saveGroup = '/msg-push-service/msgPushTask/group/save',
  updateGroup = '/msg-push-service/msgPushTask/group/update',
  batchDeleteGroup = '/msg-push-service/msgPushTask/group/batchDelete',
  deleteGroup = '/msg-push-service/msgPushTask/group/delete',

  listTaskSubscriber = '/msg-push-service/msgReceiver/listTaskSubscriber',
  saveTaskSubscriber = '/msg-push-service/msgReceiver/saveTaskSubscriber',
  deleteTaskSubscriber = '/msg-push-service/msgReceiver/deleteTaskSubscriber',
}

export const listTask = (params) => {
  return defHttp.get({ url: Api.list, params });
}

export const listMsgField = (params) => {
  return defHttp.get({ url: Api.listMsgField, params });
}

export const listTemplateByTaskId = (params) => {
  return defHttp.get({ url: Api.listTemplateByTaskId, params });
}

/**
 * 查询全部渠道
 * @param params
 */
export const listChannel = () => {
  return defHttp.get({ url: Api.listChannel, params: {} });
}


/**
 * 保存或者更新
 * @param params
 */
export const saveOrUpdateTask = (params, isUpdate) => {
  let url = isUpdate ? Api.edit : Api.save;
  return defHttp.post({ url: url, params });
}

export const saveOrUpdateMsgField = (taskId, params) => {
  return defHttp.post({ url: `${Api.saveOrUpdateMsgField}/${taskId}`, params });
}

/**
 * 查询详情
 * @param params
 */
export const getTask = (params) => {
  return defHttp.get({ url: Api.get, params });
}

/**
 * 删除
 * @param params
 */
export const deleteTask = (params, handleSuccess) => {
  return defHttp.delete({ url: Api.delete, data: params }, { joinParamsToUrl: true }).then(() => {
    handleSuccess();
  });
}

/**
 * 批量删除
 * @param params
 */
export const batchDeleteTask = (params, handleSuccess) => {
  Modal.confirm({
    title: '确认删除',
    content: '是否删除选中数据',
    okText: '确认',
    cancelText: '取消',
    onOk: () => {
      return defHttp.delete({ url: Api.deleteBatch, data: params }, { joinParamsToUrl: true }).then(() => {
        handleSuccess();
      });
    }
  });
}




export const saveOrUpdateGroup = (params, isUpdate) => {
  let url = isUpdate ? Api.updateGroup : Api.saveGroup;
  return defHttp.post({ url: url, params });
}

export const deleteGroup = (params, handleSuccess) => {
  Modal.confirm({
    title: `确认删除${params.name}`,
    content: '是否删除选中数据',
    okText: '确认',
    cancelText: '取消',
    onOk: () => {
      return defHttp.delete({ url: Api.deleteGroup, data: {id: params.id} }, { joinParamsToUrl: true }).then(() => {
        handleSuccess();
      });
    }
  });
}

export const batchDeleteGroup = (params, handleSuccess) => {
  Modal.confirm({
    title: `确认删除`,
    content: '是否删除选中数据',
    okText: '确认',
    cancelText: '取消',
    onOk: () => {
      return defHttp.delete({ url: Api.batchDeleteGroup, data: params }, { joinParamsToUrl: true }).then(() => {
        handleSuccess();
      });
    }
  });
}


/**
 * 任务订阅者
 */
export const listTaskSubscriber = (params) => defHttp.get({ url: Api.listTaskSubscriber, params })
export const saveTaskSubscriber = (params) => defHttp.post({ url: Api.saveTaskSubscriber, params })
export const deleteTaskSubscriber = (params, handleSuccess) => {
  Modal.confirm({
    title: `确认移除${params.name}`,
    content: '是否移除选中数据',
    okText: '确认',
    cancelText: '取消',
    onOk: () => {
      return defHttp.delete({ url: Api.deleteTaskSubscriber, data: {...params} }, { joinParamsToUrl: true }).then(() => {
        handleSuccess();
      });
    }
  });
}
